package com.java.zhuiyun.api.atRegularTime.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.zhuiyun.annotation.OptLog;
import com.java.zhuiyun.api.playerinformation.entity.PlayerInformation;
import com.java.zhuiyun.api.playerinformation.mapper.PlayerInformationMapper;
import com.java.zhuiyun.api.playerinformation.service.PlayerInformationService;
import com.java.zhuiyun.api.playerLoginLogs.service.PlayerLoginLogsService;
import com.java.zhuiyun.api.task.entity.Task;
import com.java.zhuiyun.api.task.mapper.TaskMapper;
import com.java.zhuiyun.api.task.service.ITaskService;
import com.java.zhuiyun.common.response.CommonConstants;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName AtRegularTimeController
 * @Description TODO 定时任务
 * @Author WX
 * @Date 2023/9/18 9:39
 **/
@Component
public class AtRegularTimeController {

    @Autowired
    private PlayerInformationService playerInformationService;

    @Autowired
    private PlayerLoginLogsService playerLoginLogsService;

    @Resource
    private PlayerInformationMapper playerInformationMapper;

    /*
    * @Author WX
    * @Description 每天凌晨4点触发修改冒险关卡
    * @Date 2023/9/18 9:48
    * @Return
    * @param null
    */
    // 定义时间格式 每天4点触发任务
    @Scheduled(cron = "0 0 4 * * ?")
    @ApiOperation(value = "每天凌晨4点触发修改冒险关卡")
    @OptLog(modular = "定时任务",type = CommonConstants.UPDATE,describe = "每天凌晨4点触发修改冒险关卡")
    public void numberOfModifications () {
        int i = updateNumberOfModifications();
        System.out.println("定时任务执行成功，并重置了" + i + "条冒险次数数据");
    }

    /*
    * @Author WX
    * @Description 每天凌晨0：01统计前日活跃数据
    * @Date 2023/9/19 8:41
    * @Return
    * @param null
    */
    @Scheduled(cron = "0 1 0 * * ?")
    @ApiOperation(value = "每天凌晨0：01统计前日活跃数据")
    @OptLog(modular = "定时任务",type = CommonConstants.SELECT,describe = "每天凌晨0：01统计前日活跃数据")
    public void activityStatistics () {
        int a = playerLoginLogsService.insertPlayerLoginLogs();
        System.out.println("定时任务执行成功，并添置" + a + "条登录数据");
    }

    /*
    * @Author WX
    * @Description 冒险关卡次数修改方法
    * @Date 2023/9/18 9:49
    * @Return
    * @param null
    */
    @ApiOperation(value = "冒险关卡次数修改方法")
    public int updateNumberOfModifications () {
        return playerInformationService.updateNumberOfModifications();
    }

    /**
    * @Author xy
    * @Description todo 每天凌晨5点刷新任务
    * @Date 2023/10/16 9:32
    * @Return
    * @param
    */
    @Scheduled(cron = "0 0 5 * * ?")
    @ApiOperation(value = "每天凌晨5点刷新任务")
    @OptLog(modular = "定时任务",type = CommonConstants.UPDATE,describe = "每天凌晨5点刷新任务")
    public void RefreshTask () {
        // 查询所有用户的任务进度值
        List<PlayerInformation> playerInformations = playerInformationMapper.selectList(null);

        // 将所有任务的completion字段设置为0
        for (PlayerInformation playerInformation : playerInformations) {
            playerInformation.setProgress("0,0,0");
            playerInformationMapper.updateById(playerInformation);
        }

        int i = playerInformations.size();
        System.out.println("定时任务执行成功，并重置了" + i + "条每日任务数据");
    }
}
